package com.ofhb.bean;

import java.util.List;
import java.util.logging.Logger;

import javax.ejb.LocalBean;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import com.ofhb.entities.HallBookingEntity;
import com.ofhb.entities.HallEntity;
import com.ofhb.entities.HallPriceEntity;
import com.ofhb.entities.UserEntity;
import com.ofhb.entities.UserTypeEntity;

/**
 * Session Bean implementation class DataReaderBean
 */
@Stateless(name="DataReaderBean",mappedName = "ejb/DataReaderBean")
@LocalBean
public class DataReaderBean implements DataReaderBeanRemote {

    Logger log = Logger.getLogger(DataReaderBean.class.getName());

    @PersistenceContext(name = "my_persistence_ctx")
    EntityManager em;
	
    /**
     * Default constructor. 
     */
    public DataReaderBean() {
        // TODO Auto-generated constructor stub
    }

    @Override
    public List<UserEntity> getAllUsers() {
    	 @SuppressWarnings("unchecked")
		List<UserEntity> listUserEntities = (List<UserEntity>)em.createQuery(
    	    	    "SELECT e FROM UserEntity e")
    	    	    .getResultList();
    	return listUserEntities;
    }
	
	@Override
	public List<UserTypeEntity> getAllUserTypes() {
		@SuppressWarnings("unchecked")
		List<UserTypeEntity> listUserTypeEntities = (List<UserTypeEntity>)em.createQuery(
	    	    "SELECT e FROM UserTypeEntity e")
	    	    .getResultList();
	return listUserTypeEntities;
	}

	@Override
	public List<HallEntity> getAllHalls() {
		@SuppressWarnings("unchecked")
		List<HallEntity> listHallEntities = (List<HallEntity>)em.createQuery(
	    	    "SELECT e FROM HallEntity e")
	    	    .getResultList();
	return listHallEntities;
	}
	
	@Override
	public List<HallBookingEntity> getAllHallBookings() {
		@SuppressWarnings("unchecked")
		List<HallBookingEntity> listHallBookingEntities = (List<HallBookingEntity>)em.createQuery(
	    	    "SELECT e FROM HallBookingEntity e")
	    	    .getResultList();
	return listHallBookingEntities;
	}
	
	@Override
	public List<HallPriceEntity> getAllHallPrices() {
		@SuppressWarnings("unchecked")
		List<HallPriceEntity> listPriceEntities = (List<HallPriceEntity>)em.createQuery(
	    	    "SELECT e FROM HallPriceEntity e")
	    	    .getResultList();
	return listPriceEntities;
	}
	
	@Override
	public boolean isUserNameExists(String userName) {
		boolean result = false;
		Query query = em.createQuery("SELECT e FROM UserEntity e where e.userName = :userName");
		query.setParameter("userName", userName);
		query.setMaxResults(1);
		if(query.getResultList().size() > 0)
			return true;
		return result;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<UserEntity> getAllOwners() {
		List<UserEntity> listUserEntities = (List<UserEntity>)em.createQuery(
	    	    "SELECT e FROM UserEntity as e JOIN e.userTypeEntity userTypeEntity where userTypeEntity.typeName = 'hallowner'")
	    	    .getResultList();
		return listUserEntities;
	}
}
